package contacts.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import com.eltima.components.ui.DatePicker;
import contacts.dao.impl.StudentDaoImpl;
import contacts.db.DbConnection;
import contacts.po.Student;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class FunctionFrame extends JFrame {
	private static final long serialVersionUID = -413862993199685619L;
	private static int isbn;
	private java.sql.Date sqlDate;
	public FunctionFrame() {
		JFrame f = new JFrame("日历");
		f.setSize(400, 300);
		f.setLocation(200, 200);
		f.setLayout(null);
		f.setResizable(false);
		final DatePicker datepick;
		datepick = getDatePicker();
		f.add(datepick);
		JButton b = new JButton("获取时间");
		b.setBounds(137, 183, 100, 30);
		f.add(b);
		b.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				isbn = ContactsFrame.isbn;
				JOptionPane.showMessageDialog(f, "获取控件中的日期：" + datepick.getValue());
				System.out.println(datepick.getValue());// 这是一个java.util.Date对象
				String dateIn = String.valueOf(datepick.getValue());
				Date dt = parse(dateIn, "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
				String str = new SimpleDateFormat("yyyy-MM-dd").format(dt);
				Date date = null;
				try {
					date = new SimpleDateFormat("yyyy-MM-dd").parse(str);
					sqlDate = new java.sql.Date(date.getTime());
					System.out.println(sqlDate); // 输出设置的时间
				} catch (ParseException e2) {
					e2.printStackTrace();
				}
				// 通过sql语句根据学生编号修改学生表里的学生的生日
				Connection conn = DbConnection.getConnection();
				String sql = "UPDATE student SET student_birthday = ? WHERE student_id = ?";
				PreparedStatement ps = null;
				try {
					ps = conn.prepareStatement(sql);
					ps.setDate(1, sqlDate);
					ps.setInt(2, (ContactsFrame.isbn) + 1);
					ps.executeUpdate();
				} catch (SQLException e1) {
					e1.printStackTrace();
				} finally {
					DbConnection.close(null, ps);
				}
			}
		});
		f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		f.setResizable(false);
		f.setVisible(true);
	}

	public DatePicker getDatePicker() {
		final DatePicker datepick;
		// 格式
		String DefaultFormat = "yyyy-MM-dd HH:mm:ss";
		// 当前时间
		// Date date = new Date();
		StudentDaoImpl studentDao = new StudentDaoImpl();
		List<Student> studentList = new ArrayList<Student>();
		isbn = ContactsFrame.isbn;
		studentList = studentDao.findAll();
		// 得到某一行的联系人的生日
		Date date = studentList.get(isbn).getBirthday();
		// 字体
		Font font = new Font("Times New Roman", Font.BOLD, 14);
		Dimension dimension = new Dimension(177, 24);
		int[] hilightDays = { 1, 3, 5, 7 };
		int[] disabledDays = { 4, 6, 5, 9 };
		// 构造方法（初始时间，时间显示格式，字体，控件大小）
		datepick = new DatePicker(date, DefaultFormat, font, dimension);
		datepick.setLocation(137, 83);// 设置起始位置
		/*
		 * //也可用setBounds()直接设置大小与位置 datepick.setBounds(137, 83, 177, 24);
		 */
		// 设置一个月份中需要高亮显示的日子
		datepick.setHightlightdays(hilightDays, Color.red);
		// 设置一个月份中不需要的日子，呈灰色显示
		datepick.setDisableddays(disabledDays);
		// 设置国家
		datepick.setLocale(Locale.CANADA);
		// 设置时钟面板可见
		datepick.setTimePanleVisible(true);
		return datepick;
	}

// 格林威治时间转Date
	private Date parse(String str, String pattern, Locale locale) {
		if (str == null || pattern == null) {
			return null;
		}
		try {
			return new SimpleDateFormat(pattern, locale).parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
